package com.afc.qua;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Vector;

import com.afc.qua.Joke.DefaultJoke;

/**
 * JokeDao interface. .
 */
public interface JokeDao
{
  /**
   * @param aJoke
   *          .
   * @return create
   */
  int create(Joke aJoke);
  /**
   * @param aKeyWord
   *          .
   * @return the Jokes
   */
  List<Joke> retrieveByKeyWord(String aKeyWord);
  /**
   * @param anId
   *          .
   */
  void delete(int anId);

  /**
   * DefaultJokeDao class. .
   */
  public class DefaultJokeDao implements JokeDao
  {
    @Override
    public int create(Joke aJoke)
    {
      int id = 0;
      try
      {
        Connection connection = JdbcUtil.getConnection();
        String query = "insert into Joke values(?, ?, ?, ?, ?)";
        PreparedStatement preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
        int index = 1;
        preparedStatement.setInt(index++, 0);
        preparedStatement.setString(index++, aJoke.getAuthor());
        preparedStatement.setString(index++, aJoke.getTitle());
        preparedStatement.setString(index++, aJoke.getContent());
        preparedStatement.setDate(index++, new Date(aJoke.getCreationDate().getTime()));
        int count = preparedStatement.executeUpdate();
        if (count == 1)
        {
          ResultSet resultSet = preparedStatement.getGeneratedKeys();
          if (resultSet.next())
          {
            id = resultSet.getInt("GENERATED_KEY");
          }
          else
          {
            throw new SQLException("Unable to create joke");
          }
        }
        else
        {
          throw new SQLException("Unable to create joke");
        }
        preparedStatement.close();
        connection.close();
      }
      catch (SQLException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      return id;
    }

    @Override
    public List<Joke> retrieveByKeyWord(String aKeyWord)
    {
      List<Joke> jokes = new Vector<>();
      try
      {
        Connection connection = JdbcUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement("select * from Joke where content like '%" + aKeyWord + "%'");
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next())
        {
          Joke joke = new DefaultJoke();
          joke.setId(resultSet.getInt("id"));
          joke.setAuthor(resultSet.getString("author"));
          joke.setTitle(resultSet.getString("title"));
          joke.setContent(resultSet.getString("content"));
          joke.setCreationDate(new java.util.Date(resultSet.getDate("creationDate").getTime()));
          jokes.add(joke);
        }
        preparedStatement.close();
        connection.close();
      }
      catch (SQLException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      return jokes;
    }

    @Override
    public void delete(int anId)
    {
      try
      {
        Connection connection = JdbcUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement("delete from Joke where id=?");
        int index = 1;
        preparedStatement.setInt(index++, anId);
        int count = preparedStatement.executeUpdate();
        preparedStatement.close();
        connection.close();
        if (count == 0)
        {
          throw new RuntimeException();
        }
      }
      catch (SQLException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
}
